// Tierless
// Copyright © 2010 Alphora
// This file is licensed under a modified BSD-license (see tierless_license.txt).
using System;

using Tierless.Framework.EntityTypes;

namespace Tierless.Framework.Entities
{
	public class Int16Column : EntityColumn
	{
		public Int16Column(Entity entity, EntityTypeColumn columnType, int columnIndex)
			: base(entity, columnType, columnIndex)
		{}

		protected override object InternalSetValue(object tempValue)
		{
			if (tempValue == null)
				return tempValue;

			if (tempValue == DBNull.Value)
				return null;

			if (!(tempValue is short))
				throw new ArgumentException("Value must be set to a valid short value.");

			return tempValue;
		}

		protected override object InternalConvertValue(object tempValue)
		{
			return Convert.ToInt16(tempValue);
		}

		protected override TypeCode InternalGetTypeCode()
		{
			return TypeCode.Int16;
		}

		protected override Type InternalGetBindingType()
		{
			return typeof (short?);
		}

		protected override string InternalGetBindingPropertyName()
		{
			return "AsDisplayString";
		}

		public override short AsShort
		{
			get { return base.AsShort; }
			set { SetValue(value); }
		}
	}
}